package com.bootdo.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.ConfirmImgDO;
import com.bootdo.system.service.ConfirmImgService;
import com.bootdo.system.vo.DeliveryConfirmDetailVO;
import net.sf.json.JSONArray;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.ConfirmListDO;
import com.bootdo.system.service.ConfirmListService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 发货确认表
 * 
 * @author xm
 * @email 1992lcg@163.com
 * @date 2021-12-27 14:38:09
 */
 
@Controller
@RequestMapping("/system/confirmList")
public class ConfirmListController {
	@Autowired
	private ConfirmListService confirmListService;
	@Autowired
	private ConfirmImgService confirmImgService;
	
	@GetMapping()
	@RequiresPermissions("system:confirmList:confirmList")
	String ConfirmList(){
	    return "system/confirmList/confirmList";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:confirmList:confirmList")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DeliveryConfirmDetailVO> confirmListList = confirmListService.list(query);
		int total = confirmListService.count(query);
		PageUtils pageUtils = new PageUtils(confirmListList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:confirmList:add")
	String add(){
	    return "system/confirmList/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:confirmList:edit")
	String edit(@PathVariable("id") String id,Model model){
		ConfirmListDO confirmList = confirmListService.get(id);
		model.addAttribute("confirmList", confirmList);
	    return "system/confirmList/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:confirmList:add")
	public R save( ConfirmListDO confirmList){
		if(confirmListService.save(confirmList)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:confirmList:edit")
	public R update( ConfirmListDO confirmList){
		confirmListService.update(confirmList);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:confirmList:remove")
	public R remove( String id){
		if(confirmListService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 批量删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:confirmList:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		confirmListService.batchRemove(ids);
		return R.ok();
	}

	@Log("导入")
	@PostMapping(value = "/import")
	@ResponseBody
	public R importInfo(@RequestParam("file") MultipartFile file, HttpServletRequest req, HttpServletResponse resp) throws Exception {
		try {
			confirmListService.importInfo(file, ShiroUtils.getUser().getUserId());
			return R.ok();
		} catch (Exception e) {
			return R.error(e.getMessage());
		}
	}

	/**
	 * 详情
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/detail/{id}")
	public String detail(@PathVariable("id") String id, Model model) {
		DeliveryConfirmDetailVO deliveryConfrimDetailVO = confirmListService.detail(id);
		model.addAttribute("info", deliveryConfrimDetailVO);
    	//根据传递单号查回执单
		List<ConfirmImgDO> reportImgDOS = new ArrayList<>();
		reportImgDOS = confirmImgService.getImgInfo(deliveryConfrimDetailVO.getExpressNo());
		if(!reportImgDOS.isEmpty()){
			deliveryConfrimDetailVO.setImgUrls(reportImgDOS);
		}
		JSONArray json = JSONArray.fromObject(reportImgDOS);
		model.addAttribute("reportImgDOS", json);
		return "system/confirmList/detail";
	}

	/**
	 * 查看回执单
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/checkImg/{id}")
	String checkImg(@PathVariable("id") String id, Model model){
		//获取发货确认单列表信息
		ConfirmListDO confirmListDO = confirmListService.get(id);
		List<ConfirmImgDO> reportImgDOS = new ArrayList<>();
		//根据传递单号查回执单图片
		reportImgDOS = confirmImgService.getImgInfo(confirmListDO.getExpressNo());
		JSONArray json = JSONArray.fromObject(reportImgDOS);
		model.addAttribute("reportImgDOS", json);
		return "system/list/checkImg";
	}

	/**
	 * 更新打印次数
	 * @param ids
	 */
	@RequestMapping({"/updatePrintNum"})
	@ResponseBody
	public void updatePrintNum(@RequestParam("ids[]") String[] ids) {
		confirmListService.updatePrintNum(ids);
	}
}
